package com.hao.crm.util;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.springframework.beans.factory.annotation.Autowired;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hao.crm.domain.Employee;
import com.hao.crm.domain.Systemlog;
import com.hao.crm.service.ISystemLogService;

public class LogUtil {
	@Autowired
	private ISystemLogService systemLogService;
	public void writeLog(JoinPoint joinPoint) throws JsonProcessingException {
		if(joinPoint.getTarget() instanceof ISystemLogService) {
			return;
		}
		System.out.println("系统日志...");
		HttpServletRequest request = UserContext.get();
		Employee opuser = (Employee) request.getSession().getAttribute(UserContext.USER_IN_SESSION);
		Systemlog log = new Systemlog();
		log.setOptime(new Date());
		log.setOpuser(opuser);
		Object target = joinPoint.getTarget();
		Signature singature = joinPoint.getSignature();
		String function = target.getClass().getName()+":"+singature.getName();
		log.setFunction(function);
		log.setOpip(request.getRemoteAddr());
		ObjectMapper mapper = new ObjectMapper();
		mapper.setSerializationInclusion(Include.NON_NULL);
		String paramsValue = mapper.writeValueAsString(joinPoint.getArgs());
		log.setParams(paramsValue);
		systemLogService.save(log);
	}
}
